<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.DateFormat" %>
<jsp:include page="head.jsp" />
<% 

String un = request.getParameter("uname");
String pw = request.getParameter("passwd");
//Fraser HOgg helping with session bug
if(un == null){
	un = (String)session.getAttribute("username");
	pw = (String)session.getAttribute("pwd");
}

//Mike Earl
//declare variables for customer info that will be used in the HTML;
String firstName = null;
String lastName = null;
String address = null;
String city = null;
String prov = null;
String postal = null;
String country = null;
String homePhone = null;
String busPhone = null;
String email = null;
String password = null;
String bookingNo = null;
String bookingDate = null;
String travellerCount = null;
DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG);
Connection conn = null;
Statement stmt = null;
if(session.getAttribute("username") != null){
	un = (String)session.getAttribute("username");
	pw = (String) session.getAttribute("pass");
}else {
		session.setAttribute("success","");
}
String sql = "Select * from Customers where CustEmail='" + un + "' and CustPasswd='" + pw  + "'";

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/travelexperts","root","root");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
{
	
	firstName = rs.getString(2);
	lastName = rs.getString(3);
	address = rs.getString(4);
	city = rs.getString(5);
	prov = rs.getString(6);
	postal = rs.getString(7);
	country = rs.getString(8);
	homePhone = rs.getString(9);
	busPhone = rs.getString(10);
	email = rs.getString(11);
	password = rs.getString(13);
	session.setAttribute("username", un);
	session.setAttribute("pass", pw);
}
else {
	
	response.sendRedirect("login.jsp");
	
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
conn.close();
stmt.close();
}
%>
<!-- green header strip with login and my account links -->
	<div id="header-wrap" class="wrap">
		<div id="green-header" class="container">
			<div id="customer-links" class="pull-right">
				<% 
				String logoutbtn = null;
				if (session.getAttribute("username") == null){
					logoutbtn = "login";
						}
						else{
							
						logoutbtn = "logout";
				}
%>				
				<a href="<%=logoutbtn %>.jsp"><%=logoutbtn %></a>
				<a href="account.jsp">My Account</a>
				<a href="registration.jsp">Sign-Up</a>
			</div>
		</div>
	</div>
<!-- the row-end div is to take care of the floating issue -->
	<div class="row-end"></div>
<!-- This is the main content of the "page" 978px wide -->

	<header class="container">
	<div class="col3" id="logo">
		<img src="images/TravelLogo.jpg" alt="travel experts" />
	</div>
	<div class="col9" id="main-nav">
		<ul class="pull-right">
			<li><a href="index.jsp">Home</a></li>
			<li><a href="index.jsp">Packages</a></li>
			<li><a href="index.jsp">Specials</a></li>
			<li><a href="index.jsp">blog</a></li>
			<li><a href="index.jsp">Contact</a></li>
		</ul>
	</div>
	</header>
	<div class="row-end"></div>
<div id="main" role="main" class="container">
<div class="col4 first">
 <h2>Hello <small><%=session.getAttribute("username") %></small></h2>
</div>
<div class="col8">
<ul class="custInfo col3 first">
	<li><strong>Name:</strong> <%=firstName %> <%=lastName %></li>
	<li><strong>Home Phone:</strong> <%= homePhone%></li>
	<li><strong>Bus Phone:</strong> <%= busPhone %></li>
	<li><strong>Email:</strong> <%= email%></li>
	 </ul>
	<ul class="custInfo col3">
	<li><strong>Address:</strong> <%= address%></li>
	<li><strong>City:</strong> <%= city%></li>
	<li> <strong>Province:</strong> <%=prov %></li>
	<li><strong>Country:</strong> <%= country%></li>
	<li><strong>Postal Code:</strong> <%=postal %></li>
	 
	
</ul>
<div class="row-end"></div>
<div>
<h3 id="toggler"><img src="images/edit_pencil.png" alt="edit icon" />Edit Info</h3>
<div id="updateBlock">
<div class="success">
	<%=session.getAttribute("success")%>
</div>
<form action="update.jsp" method="get" id="updateForm">
<div class="col3">
<label>First Name:</label>
<input type="text" name="uname" id="ufname" value="<%=firstName%>" />
</div>
<div class="col3">
<label>Last Name: </label>
<input type="text" name="ulname" id="ulname" value="<%=lastName%>" />
</div>
<div class="col3 first">
<label>Address: </label>
<input type="text" name="uaddress" id="uaddress" value="<%=address%>" />
</div>
<div class="col3">
<label>City: </label>
<input type="text" name="ucity" id="ucity" value="<%=city%>" />
</div>
<div class="col3 first">
<label>Province: </label>
<input type="text" name="uprov" id="uprov" value="<%=prov%>" maxlength="2" />
</div>
<div class="col3">
<label>Postal Code: </label>
<input type="text" name="upostal" id="upostal" value="<%=postal%>" />
</div>
<div class="col3 first">
<label>Country: </label>
<input type="text" name="ucountry" id="ucountry" value="<%=country%>" />
</div>
<div class="col3">
<label>Home Phone: </label>
<input type="tel" name="uhomephone" id="uhomphone" value="<%=homePhone%>" />
</div>
<div class="col3 first">
<label>Business Phone: </label>
<input type="tel" name="ubusphone" id="ubusphone"  value="<%=busPhone %>" />
</div>
<div class="col3">
<label>Email: </label>
<input type="email" name="uemail" id="uemail"  value="<%=email%>" />
</div>
<div class="col3 first">
<label>Password: </label>
<input type="password" name="upasswd" id="upasswd"  value="<%=password%>" />
</div>
<div class="col3">
<label>Confirm Password: </label>
<input type="password" name="uconfirmPasswd" id="confirmPasswd" value="<%= password %>" />
</div>
<div class="row-end"></div>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</div>
</form>
</div>
</div>

<div class="row-end"></div>
<div class="col4 first">
<h2>Booking Info:</h2>
</div>
<div class="col8">
<table id="bookingTable">
<tr><th>Booking Number</th><th>Booking Date</th><th>Travellers Count</th></tr>
<% 
//Mike Earl
//declare variables for customer info that will be used in the HTML;
String sqlb = "Select * from Bookings b, Customers c where c.CustEmail = '" + un + "' and c.CustPasswd = '" + pw + "' and b.CustomerId = c.CustomerId";
try {
	
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/travelexperts","root","root");
	stmt = conn.createStatement();
	ResultSet rsb = stmt.executeQuery(sqlb);
	while(rsb.next())
	{	
		bookingDate = formatter.format(rsb.getDate(2));
		bookingNo = rsb.getString(3);
		travellerCount = rsb.getString(4);
		out.println( "<tr><td>" + bookingNo + "</td><td>" + bookingDate + "</td><td>" + travellerCount + "</td></tr>");
	}	
}catch(Exception e) {
	e.printStackTrace();
}
finally {
conn.close();
stmt.close();
}

%>
</table>
</div>
</div>
<!-- END Main -->
<!-- the row-end div is to take care of the floating issue -->
<div class="row-end"></div>
<div id="push"></div>
</div><!-- END OF Spacer -->
<jsp:include page="footer.jsp" />